package net.zdsoft.wpchat.android.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间相关类
 * 
 * @author xuan
 * @version $Revision: 1.0 $, $Date: 2014-9-22 下午10:40:08 $
 */
public class DateUtil {

    /**
     * 获取当天日期， 返回时间格斯为"yyyyMMdd"
     * 
     * @return tDate;
     */
    public static String getTodayByyyyyMMdd() {
        Calendar ctime = Calendar.getInstance();
        SimpleDateFormat fymd = new SimpleDateFormat("yyyyMMdd");
        Date date = ctime.getTime();
        String tDate = fymd.format(date);
        return tDate;
    }

    /**
     * 间隔时间，返回间隔多少天
     * 
     * @param lastDay
     * @param today
     * @return
     * @throws ParseException
     */
    public static int getIntervalDays(String lastDay, String today) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        int day = 0;
        java.util.Date now = df.parse(today);
        java.util.Date date = df.parse(lastDay);
        long l = 0;
        if (now.getTime() > date.getTime()) {
            l = now.getTime() - date.getTime();
        }
        else {
            l = date.getTime() - now.getTime();
        }
        day = (int) (l / (24 * 60 * 60 * 1000));
        return day;
    }

    /**
     * 获取当前时间， 返回时间格斯为"yyyyMMdd HH:mm:ss"
     * 
     * @return tDate;
     */
    public static String getNow() {
        Calendar ctime = Calendar.getInstance();
        SimpleDateFormat fymd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = ctime.getTime();
        String tDate = fymd.format(date);
        return tDate;
    }

    /**
     * 间隔时间，返回间隔多少小时
     * 
     * @param lastDay
     * @param today
     * @return
     * @throws ParseException
     */
    public static int getIntervalHours(String lastDay, String today) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int hour = 0;
        java.util.Date now = df.parse(today);
        java.util.Date date = df.parse(lastDay);
        long l = 0;
        if (now.getTime() > date.getTime()) {
            l = now.getTime() - date.getTime();
        }
        else {
            l = date.getTime() - now.getTime();
        }
        hour = (int) (l / (60 * 60 * 1000));
        return hour;
    }

    /**
     * 间隔时间，返回间隔多少小时
     * 
     * @param lastDay
     * @param today
     * @return
     * @throws ParseException
     */
    public static long compareSecond(Date firstTime, Date lastTime) throws ParseException {
        long l = 0;
        l = firstTime.getTime() - lastTime.getTime();
        return l;
    }

    public static void main(String[] args) {
        String s1 = "2013-02-27 12:00:00";
        String s2 = "2013-02-28 12:59:59";

        try {
            System.out.println(getIntervalHours(s1, s2));
        }
        catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
